package yxy.game.pm2.command.kow;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.packagedata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.RankWorship;
import yxy.game.pm2.bean.module.rank.RankWorshipPlayer;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.KowMessage;
import yxy.game.pm2.module.RankWorshipHandle;
import yxy.game.pm2.module.kow.RankKowKing;
import yxy.game.pm2.module.player.PlayerHandle;

/** 天下第一 天下第一霸主点赞 */
@MessageType(proto = Command.KowKingFan, classtype = KowMessage.KowKingFanRequest.class)
public class CMD_KowKingFan extends Command<KowMessage.KowKingFanRequest> {

	@Override
	public void execute(MessageSource messageSource, KowMessage.KowKingFanRequest request) {
		String playerCode = messageSource.getTag();
		String kingCode = request.getCode();

		KowMessage.KowKingFanResponse.Builder response = KowMessage.KowKingFanResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

		RankKowKing rankKowKing = new RankKowKing(areaBean.getGroupId());
		RankEntry entry = rankKowKing.get(kingCode);
		if (entry == null) {
			send(messageSource, response, Result.ParamError);
			return;
		}
		RankWorshipHandle.RankWorshipEntry worshipEntry = new RankWorshipHandle.RankWorshipEntry();

		RankWorship worship = rankKowKing.getWorship();

		packagedata data = DataCenter.getData(2, packagedata.class);
		Currencies rewards = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));

		int ranking = rankKowKing.rank(kingCode);
		Result result = new RankWorshipHandle().worship(playerCode, ranking, rankKowKing, rewards, worshipEntry);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}

		RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

		KowMessage.KowKing.Builder builder = KowMessage.KowKing.newBuilder();
		builder.setLevel(yxy.game.pm2.message.KowMessage.KowRankLevel.forNumber(entry.getValue().intValue()));
		builder.setFans(worship.getScore(kingCode));
		builder.setLiked(worshipPlayer.getWorshiped().contains(kingCode));
		builder.setPlayerInfo(new PlayerHandle().playerSimpleInfo(kingCode));
		response.setKing(builder);
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, KowMessage.KowKingFanResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}